package com.blockmeta.bbs.businesslibrary.net.okhttp;

import android.content.Context;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.net.callback.MyStringCallback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by author on 2017/12/28.
 * <p>
 * 封装张泓洋的HttpUtils的get请求
 * 1。包括请求头
 * 2。缓存
 */

public class OKHttpGetReq {
    public Context context;
    public String url;

    /**
     * 必须手动塞入Token 和From参数！！
     *
     * @param context
     * @param url
     */
    public OKHttpGetReq(Context context, String url) {
        this.context = context;
        this.url = url;
    }

    /**
     * 必须塞入Token 和From
     * <p>
     * 同时需要写入缓存！
     */
    public void getData(final Callback callback) {
        ProgressBarUtil.showProgress(context);
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onResponse(response, id);
                    }
                });
    }

    /**
     * 列表数据预加载
     *
     * @param callback
     */
    public void getDataList(final Callback callback) {
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }

    public void getDataWithParam(final Callback callback, HashMap<String, String> map) {
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .params(map)
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }


    public void getDataWithParam(final Callback callback, String header1key, String header1value, HashMap<String, String> map) {
        ProgressBarUtil.showProgress(context);
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader(header1key, header1value)
                .params(map)
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onResponse(response, id);
                    }
                });
    }

    public void getDataWithParam(final Callback callback, String header1key, String header1value) {
        ProgressBarUtil.showProgress(context);
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader(header1key, header1value)
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        callback.onResponse(response, id);
                    }
                });
    }

    public void getDataWithParamLoadMore(final Callback callback, HashMap<String, String> map) {
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .params(map)
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onError("error");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }


    public void getDataWithUid(final Callback callback) {
        if (!AccountHelper.isLogin()) {
            return;
        }
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader("uid", AccountHelper.getUid())
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        callback.onError("error");

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }

}
